package com.novel.app.view.storys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;

import com.dongdxiang.framework.BaseActivity;
import com.dongdxiang.framework.http.QjResult;
import com.google.gson.reflect.TypeToken;
import com.novel.app.R;
import com.novel.app.utils.Configs;
import com.novel.app.view.storys.adpter.StoryAdpter;
import com.novel.app.view.storys.bean.StoryEntity;
import java.util.List;

public class StoryActivity extends BaseActivity {

    private int page = 1;
    private int cateId = 0;
    private final static String TYPE_NEW = "is_new";
    private final static String TYPE_COM = "is_finished";
    private final static String TYPE_HOT = "is_free";
    private String TYPE = TYPE_NEW;
    private StoryAdpter storyAdpter;
    private ListView listView;
    private RadioButton rd_new;
    private RadioButton rb_finsh;
    private RadioButton rd_hot;
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_story);
        setCenterTitle("书城");
        setLeft_Btn("返回",R.mipmap.s6,onClickListener);
        storyAdpter = new StoryAdpter(this);
        listView = (ListView)findViewById(R.id.listview);
        rd_new = (RadioButton) findViewById(R.id.rb_new);
        rb_finsh = (RadioButton) findViewById(R.id.rb_finsh);
        rd_hot = (RadioButton) findViewById(R.id.rb_hot);
        cateId = getIntent().getIntExtra("cateId",0);
        if (cateId == 4){
            ((RadioButton)findViewById(R.id.rb2)).setChecked(true);
            rb_finsh.setChecked(true);
        }else if(cateId == 5){
            ((RadioButton)findViewById(R.id.rb3)).setChecked(true);
            rd_hot.setChecked(true);
        }
        listView.setOnItemClickListener(onItemClickListener);
        doNet();
    }

    private void doNet(){
        String where = "";
        if (cateId >= 0 && cateId <= 3) where = "/"+cateId;
        String url = Configs.STROYS + TYPE +where+"?page="+page;
        httpGetRequest(0,url,new TypeToken<QjResult<StoryEntity>>() {},true,true);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    };

    public void onMenuChange(View view){
        switch (view.getId()){
            case R.id.rb1:
                TYPE = TYPE_NEW;
                rd_new.setChecked(true);
                break;
            case R.id.rb2:
                TYPE = TYPE_COM;
                rb_finsh.setChecked(true);
                break;
            case R.id.rb3:
                TYPE = TYPE_HOT;
                rd_hot.setChecked(true);
                break;
        }
        doNet();
    }


    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            StoryEntity.NovelEntity  novelEntity = (StoryEntity.NovelEntity)parent.getAdapter().getItem(position);
            Intent intent = new Intent(StoryActivity.this,StoryDetailActivity.class);
            intent.putExtra("id",novelEntity.getId());
            startActivity(intent);
        }
    };

    @Override
    protected void onSucceed(int what, Object response) {
        if (response != null){
            StoryEntity storyEntity = (StoryEntity)response;
            List<StoryEntity.NovelEntity> list = storyEntity.getData();
            if (list != null && list.size() > 0){
                storyAdpter.addAll(list);
                listView.setAdapter(storyAdpter);
            }
        }
    }

    @Override
    protected void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {

    }
}
